package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.UserGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class UserGroupServiceImpl implements UserGroupService {
    @Autowired
    private UserGroupRepository userGroupRepository;


    @Override
    public PageResult<UserGroupVO> pageList(UserGroupQuery query) {
        log.info("查询人群列表，入参：{}", query);
        PageResult<UserGroupVO> result = new PageResult<>();
        PageInfo<UserGroupPO> pageInfo = userGroupRepository.pagesList(query);
        log.info("查询人群列表，出参：{}",pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        result.setPageNo(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        List<UserGroupVO> vos = null;
        List<UserGroupPO> pos = pageInfo.getList();
        if (CollectionUtils.isNotEmpty(pos)) {
            vos = pos.stream().map(po -> {
                UserGroupVO vo = new UserGroupVO();
                BeanUtils.copyProperties(po, vo);
                return vo;
            }).collect(Collectors.toList());
        }
        result.setObjects(vos);
        log.info("查询人群列表1111，出参：{}", vos);
        return result;
    }

    @Override
    public void updateStatus(UserGroupParam param) {
        log.info("更新人群启用状态，入参：{}",param);
        UserGroupPO upo = userGroupRepository.queryById(param.getId());
        UserGroupPO po = new UserGroupPO();
        po.setId(param.getId());
        if (upo.getStatus()==1){
            po.setStatus(0);
            userGroupRepository.updateStatus(po);
        }else {
            po.setStatus(1);
            userGroupRepository.updateStatus(po);
        }
    }
    @Override
    public void delete(Integer id)  {
       log.info("删除人群数据，ID: {}", id);
       userGroupRepository.delete(id);
    }

    @Override
    public UserGroupVO detail(Integer id) {
        log.info("查询人群数据，入参: {}", id);
        UserGroupPO po = userGroupRepository.queryById(id);
        UserGroupVO vo = null;
        if (po != null) {
            vo = new UserGroupVO();
            BeanUtils.copyProperties(po, vo);
        }
        return  vo;
    }

    @Override
    public void updateGroup(UserGroupParam param) {
        log.info("更新人群数据，入参: {}", param);
        UserGroupPO po = new UserGroupPO();
        BeanUtils.copyProperties(param, po);
        po.setStatus(1);
        log.info("更新人群数据，入参: {}", po);
        if (param.getId() == null){
            log.info("新增人群数据，入参: {}", po);
            userGroupRepository.save(po);
        }else {
            userGroupRepository.update(po);
        }
    }


    @Override
    public UserGroupVO queryById(Integer id) {
        log.info("查询人群数据，ID: {}", id);
        UserGroupPO po = userGroupRepository.queryById(id);
        UserGroupVO vo = new UserGroupVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }


}



